package com.judian.smart.util;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.blankj.utilcode.util.ObjectUtils;
import com.judian.smart.model.Device;
import com.judian.smart.model.Floor;
import com.judian.smart.model.GateWay;
import com.judian.smart.model.Mode;
import com.judian.smart.model.ModeList;
import com.judian.smart.model.RedInfra;
import com.judian.smart.model.Room;
import com.judian.smart.model.User;
import com.tencent.bugly.crashreport.CrashReport;

import java.util.ArrayList;
import java.util.List;

import static com.judian.smart.db.WidgetDAOImpl.TABLE_DEVICE;
import static com.judian.smart.db.WidgetDAOImpl.TABLE_FLOOR;
import static com.judian.smart.db.WidgetDAOImpl.TABLE_GATEWAY;
import static com.judian.smart.db.WidgetDAOImpl.TABLE_INFRARED;
import static com.judian.smart.db.WidgetDAOImpl.TABLE_MODE;
import static com.judian.smart.db.WidgetDAOImpl.TABLE_MODE_LIST;
import static com.judian.smart.db.WidgetDAOImpl.TABLE_ROOM;
import static com.judian.smart.db.WidgetDAOImpl.TABLE_USER;

//Ling 2019.9.16 及时关闭cursor，修复数据库更新报“OS error - 24:Too many open files”的bug
//Ling 2019.7.25 方便统一更改版本更新及方案下载时处理数据库的改动
//Ling 2019.9.26 加入字段存在判断，兼容低版本数据库
public class UpdataAndDownload {
    private static String[] tablaNames = new String[]{"t_room", "t_mode", "t_user", "t_mode_list", "t_device", "t_gateway", "t_infrared", "t_floor"};

    //Ling 2019.8.7 更改判断数据库是否为空、数据库某张表是否存在的判断方式
    public static boolean isHaveTable(SQLiteDatabase db) {
        List<String> stringList = new ArrayList<>();
        Cursor cursor;
        if (db == null) {
            return false;
        } else {
            cursor = db.rawQuery("select name from sqlite_master where type='table';", null);
            while (cursor.moveToNext()) {
                //遍历出表名
                String name = cursor.getString(0);
                Log.d("zhanglifan", "isHaveTable:   = " + name);
                stringList.add(name);
            }
            for (int i = 0; i < tablaNames.length; i++) {
                if (!stringList.contains(tablaNames[i])) {
                    cursor.close();
                    return false;
                }
            }
        }
        cursor.close();
        return true;
    }

    public static List<Room> getRoom(SQLiteDatabase db) {
        List<Room> list = new ArrayList<Room>();
        if (db != null) {
            try {
                String sql = "select * from t_room";
                Cursor cursor = db.rawQuery(sql, null);
                int resultCount = cursor.getCount();
                cursor.moveToFirst();
                for (int i = 0; i < resultCount; i++) {
                    Room room = new Room();
                    int index;
                    //if((index=cursor.getColumnIndex("Id"))!=-1)
                    room.setId(cursor.getInt(0));
                    if ((index = cursor.getColumnIndex("RoomName")) != -1)
                        room.setRoomName(cursor.getString(index));
                    if ((index = cursor.getColumnIndex("RoomImage")) != -1)
                        room.setRoomImage(cursor.getString(index));
                    if ((index = cursor.getColumnIndex("IsShow")) != -1)
                        room.setIsShow(cursor.getInt(index));
                    if ((index = cursor.getColumnIndex("CreationTime")) != -1)
                        room.setCreationTime(cursor.getString(index));
                    if ((index = cursor.getColumnIndex("Seqencing")) != -1)
                        room.setSeqencing(cursor.getInt(index));
                    if ((index = cursor.getColumnIndex("Data1")) != -1)
                        room.setData1(cursor.getString(index));
                    if ((index = cursor.getColumnIndex("LanguageId")) != -1)
                        room.setLanguageId(cursor.getInt(index));
                    if ((index = cursor.getColumnIndex("CompanyId")) != -1)
                        room.setCompanyId(cursor.getInt(index));
                    if ((index = cursor.getColumnIndex("Data2")) != -1)
                        room.setData2(cursor.getString(index));
                    if ((index = cursor.getColumnIndex("floorId")) != -1) {
                        room.setFloorId(ObjectUtils.isEmpty(cursor.getInt(index)) ? 5 : cursor.getInt(index));
                    } else {
                        room.setFloorId(5);
                    }
                    if ((index = cursor.getColumnIndex("floorName")) != -1) {
                        room.setFloorName(ObjectUtils.isEmpty(cursor.getString(index)) ? "一楼" : cursor.getString(index));
                    } else {
                        room.setFloorName("一楼");
                    }
                    if ((index = cursor.getColumnIndex("avtar")) != -1) {
                        room.setAvtar(ObjectUtils.isEmpty(cursor.getString(index)) ? "0" : cursor.getString(index));
                    } else {
                        room.setAvtar("0");
                    }
                    list.add(room);
                    cursor.moveToNext();
                }
                cursor.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return list;
    }


    public static List<Device> getDevices(SQLiteDatabase db) {
        List<Device> list = new ArrayList<Device>();
        try {
            String sql = "select * FROM t_device";
            if (db != null) {
                Cursor cursor = db.rawQuery(sql, null);
                if (cursor != null) {
                    int resultCount = cursor.getCount();
                    cursor.moveToFirst();
                    for (int i = 0; i < resultCount; i++) {
                        Device device = new Device();
                        int index;
                        //if((index=cursor.getColumnIndex("DeviceName"))!=-1)
                        device.setId(cursor.getInt(0));
                        if ((index = cursor.getColumnIndex("DeviceName")) != -1)
                            device.setDeviceName(cursor.getString(index));
                        if ((index = cursor.getColumnIndex("DeviceImage")) != -1)
                            com.blankj.utilcode.util.LogUtils.dTag("setDeviceImage==", "getDevices5 = " + cursor.getString(index));
                        device.setDeviceImage(cursor.getString(index));
                        if ((index = cursor.getColumnIndex("DeviceID")) != -1)
                            device.setDeviceID(cursor.getString(index));
                        if ((index = cursor.getColumnIndex("DeviceNickName")) != -1)
                            device.setDeviceNickName(cursor.getString(index));
                        if ((index = cursor.getColumnIndex("CreationTime")) != -1)
                            device.setCreationTime(cursor.getString(index));
                        if ((index = cursor.getColumnIndex("Seqencing")) != -1)
                            device.setSeqencing(cursor.getInt(index));
                        if ((index = cursor.getColumnIndex("DeviceOnLine")) != -1)
                            device.setDeviceOnLine(cursor.getInt(index));
                        if ((index = cursor.getColumnIndex("DeviceTimer")) != -1)
                            device.setDeviceTimer(cursor.getString(index));
                        if ((index = cursor.getColumnIndex("DeviceIO")) != -1)
                            device.setDeviceIO(cursor.getString(index));
                        if ((index = cursor.getColumnIndex("DeviceOrdered")) != -1)
                            device.setDeviceOrdered(cursor.getString(index));
                        if ((index = cursor.getColumnIndex("StartTime")) != -1)
                            device.setStartTime(cursor.getString(index));
                        if ((index = cursor.getColumnIndex("EndTime")) != -1)
                            device.setEndTime(cursor.getString(index));
                        if ((index = cursor.getColumnIndex("SceneId")) != -1)
                            device.setSceneId(cursor.getInt(index));
                        if ((index = cursor.getColumnIndex("DeviceTypeKey")) != -1)
                            device.setDeviceTypeKey(cursor.getInt(index));
                        if ((index = cursor.getColumnIndex("ProductsKey")) != -1)
                            device.setProductsKey(cursor.getInt(index));
                        if ((index = cursor.getColumnIndex("GradingId")) != -1)
                            device.setGradingId(cursor.getInt(index));
                        if ((index = cursor.getColumnIndex("RoomId")) != -1)
                            device.setRoomId(cursor.getInt(index));
                        if ((index = cursor.getColumnIndex("CompanyId")) != -1)
                            device.setCompanyId(cursor.getInt(index));
                        if ((index = cursor.getColumnIndex("LanguageId")) != -1)
                            device.setLanguageId(cursor.getInt(index));
                        if ((index = cursor.getColumnIndex("RoleId")) != -1)
                            device.setRoleId(cursor.getInt(index));
                        if ((index = cursor.getColumnIndex("Data1")) != -1)
                            device.setData1(cursor.getString(index));
                        if ((index = cursor.getColumnIndex("Data2")) != -1)
                            device.setData2(cursor.getString(index));
                        if ((index = cursor.getColumnIndex("floorID")) != -1) {
                            int floorId = cursor.getInt(index);
                            device.setFloorId(ObjectUtils.isEmpty(floorId) ? 5 : floorId);
                        } else {
                            device.setFloorId(5);
                        }
                        if ((index = cursor.getColumnIndex("floorName")) != -1) {
                            String floorName = cursor.getString(index);
                            device.setFloorName(ObjectUtils.isEmpty(floorName) ? "一楼" : floorName);
                        } else {
                            device.setFloorName("一楼");
                        }
                        if ((index = cursor.getColumnIndex("allOpen")) != -1) {
                            device.setAllOpen(ObjectUtils.isEmpty(cursor.getInt(index)) ? 0 : cursor.getInt(index));
                        } else {
                            device.setAllOpen(0);
                        }
                        if ((index = cursor.getColumnIndex("allClose")) != -1) {
                            device.setAllClose(ObjectUtils.isEmpty(cursor.getInt(index)) ? 0 : cursor.getInt(index));
                        } else {
                            device.setAllClose(0);
                        }
                        device.setIsBackCode(0);
                        list.add(device);
                        cursor.moveToNext();
                    }
                    cursor.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }


    public static List<RedInfra> getInfrareds(SQLiteDatabase db) {
        List<RedInfra> list = new ArrayList<RedInfra>();
        try {
            String sql = "select * from t_infrared";
            Cursor cursor = db.rawQuery(sql, null);
            int resultCount = cursor.getCount();
            cursor.moveToFirst();
            for (int i = 0; i < resultCount; i++) {
                RedInfra infrared = new RedInfra();
                int index;
                if ((index = cursor.getColumnIndex("Id")) != -1)
                    infrared.setId(cursor.getInt(index));
                if ((index = cursor.getColumnIndex("InfraredImage")) != -1)
                    infrared.setInfraredImage(cursor.getString(index));
                if ((index = cursor.getColumnIndex("DeviceId")) != -1)
                    infrared.setDeviceId(cursor.getString(index));
                if ((index = cursor.getColumnIndex("FatherId")) != -1)
                    infrared.setFatherId(cursor.getInt(index));
                if ((index = cursor.getColumnIndex("InfraredIndex")) != -1)
                    infrared.setInfraredIndex(cursor.getInt(index));
                if ((index = cursor.getColumnIndex("InfraredBrandId")) != -1)
                    infrared.setInfraredBrandId(cursor.getInt(index));
                if ((index = cursor.getColumnIndex("InfraredBrandName")) != -1)
                    infrared.setInfraredBrandName(cursor.getString(index));
                if ((index = cursor.getColumnIndex("InfraredVersionId")) != -1)
                    infrared.setInfraredVersionId(cursor.getInt(index));
                if ((index = cursor.getColumnIndex("InfraredVersionName")) != -1)
                    infrared.setInfraredVersionName(cursor.getString(index));
                if ((index = cursor.getColumnIndex("InfraredKey")) != -1)
                    infrared.setInfraredKey(cursor.getInt(index));
                if ((index = cursor.getColumnIndex("InfraredButtonId")) != -1)
                    infrared.setInfraredButtonId(cursor.getInt(index));
                if ((index = cursor.getColumnIndex("InfraredName")) != -1)
                    infrared.setInfraredName(cursor.getString(index));
                if ((index = cursor.getColumnIndex("InfraredCode")) != -1)
                    infrared.setInfraredCode(cursor.getString(index));
                if ((index = cursor.getColumnIndex("InfraredStudyType")) != -1)
                    infrared.setInfraredStudyType(cursor.getInt(index));
                if ((index = cursor.getColumnIndex("InfraredX")) != -1)
                    infrared.setInfraredX(cursor.getInt(index));
                if ((index = cursor.getColumnIndex("InfraredY")) != -1)
                    infrared.setInfraredY(cursor.getInt(index));
                if ((index = cursor.getColumnIndex("CreationTime")) != -1)
                    infrared.setCreationTime(cursor.getString(index));
                if ((index = cursor.getColumnIndex("Seqencing")) != -1)
                    infrared.setSeqencing(cursor.getInt(index));
                list.add(infrared);
                cursor.moveToNext();
            }
            cursor.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public static List<Mode> getMode(SQLiteDatabase db) {
        List<Mode> list = new ArrayList<Mode>();
        if (db != null) {
            try {
                Cursor cursor = db.query(TABLE_MODE, null, null, null, null, null, null);
                int resultCount = cursor.getCount();
                cursor.moveToFirst();
                for (int i = 0; i < resultCount; i++) {
                    Mode mode = new Mode();
                    int index;
                    mode.setId(cursor.getInt(0));
                    if ((index = cursor.getColumnIndex("ModeName")) != -1)
                        mode.setModeName(cursor.getString(index));
                    if ((index = cursor.getColumnIndex("ModeImage")) != -1)
                        mode.setModeImage(cursor.getString(index));
                    if ((index = cursor.getColumnIndex("ModeType")) != -1)
                        mode.setModeType(cursor.getString(index));
                    if ((index = cursor.getColumnIndex("ModeCode")) != -1)
                        mode.setModeCode(cursor.getInt(index));
                    if ((index = cursor.getColumnIndex("ModeLoop")) != -1)
                        mode.setModeLoop(cursor.getInt(index));
                    if ((index = cursor.getColumnIndex("CreationTime")) != -1)
                        mode.setCreationTime(cursor.getString(index));
                    if ((index = cursor.getColumnIndex("Seqencing")) != -1)
                        mode.setSeqencing(cursor.getInt(index));
                    if ((index = cursor.getColumnIndex("StartTime")) != -1)
                        mode.setStartTime(cursor.getString(index));
                    if ((index = cursor.getColumnIndex("EndTime")) != -1)
                        mode.setEndTime(cursor.getString(index));
                    if ((index = cursor.getColumnIndex("ModeNickName")) != -1)
                        mode.setModeNickName(cursor.getString(index));
                    if ((index = cursor.getColumnIndex("GatewayID")) != -1)
                        mode.setGatewayID(cursor.getString(index));
                    if ((index = cursor.getColumnIndex("LanguageId")) != -1)
                        mode.setLanguageId(cursor.getInt(index));
                    if ((index = cursor.getColumnIndex("CompanyId")) != -1)
                        mode.setCompanyId(cursor.getInt(index));
                    if ((index = cursor.getColumnIndex("Data1")) != -1)
                        mode.setData1(cursor.getString(index));
                    if ((index = cursor.getColumnIndex("Data2")) != -1)
                        mode.setData2(cursor.getString(index));
                    if ((index = cursor.getColumnIndex("IsShow")) != -1)
                        mode.setIsShow(cursor.getInt(index));
                    if ((index = cursor.getColumnIndex("roomId")) != -1) {
                        mode.setRoomId(ObjectUtils.isEmpty(cursor.getInt(index)) ? 1 : cursor.getInt(index));
                    } else {
                        mode.setRoomId(1);
                    }
                    if ((index = cursor.getColumnIndex("roomName")) != -1) {
                        mode.setRoomName(ObjectUtils.isEmpty(cursor.getString(index)) ? "客厅" : cursor.getString(index));
                    } else {
                        mode.setRoomName("客厅");
                    }
                    if ((index = cursor.getColumnIndex("floorName")) != -1) {
                        mode.setFloorName(ObjectUtils.isEmpty(cursor.getString(index)) ? "一楼" : cursor.getString(index));
                    } else {
                        mode.setFloorName("一楼");
                    }
                    if ((index = cursor.getColumnIndex("floorId")) != -1) {
                        mode.setFloorId(ObjectUtils.isEmpty(cursor.getInt(index)) ? 5 : cursor.getInt(index));
                    } else {
                        mode.setFloorId(5);
                    }
                    list.add(mode);
                    cursor.moveToNext();
                }
                cursor.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    public static List<ModeList> getModeList(SQLiteDatabase db) {
        List<ModeList> list = new ArrayList<ModeList>();
        try {
            String sql = "select * from t_mode_list";
            if (db != null) {
                Cursor cursor = db.rawQuery(sql, null);
                if (cursor != null) {
                    int resultCount = cursor.getCount();
                    cursor.moveToFirst();
                    for (int i = 0; i < resultCount; i++) {
                        ModeList modeList = new ModeList();
                        int index;
                        if ((index = cursor.getColumnIndex("Id")) != -1)
                            modeList.setId(cursor.getInt(index));
                        if ((index = cursor.getColumnIndex("ModeAction")) != -1)
                            modeList.setModeAction(cursor.getString(index));
                        if ((index = cursor.getColumnIndex("ModeFunction")) != -1)
                            modeList.setModeFunction(cursor.getString(index));
                        if ((index = cursor.getColumnIndex("ModeTime")) != -1)
                            modeList.setModeTime(cursor.getString(index));
                        if ((index = cursor.getColumnIndex("ModeDelayed")) != -1)
                            modeList.setModeDelayed(cursor.getString(index));
                        if ((index = cursor.getColumnIndex("ModePeriod")) != -1)
                            modeList.setModePeriod(cursor.getString(index));
                        if ((index = cursor.getColumnIndex("CreationTime")) != -1)
                            modeList.setCreationTime(cursor.getString(index));
                        if ((index = cursor.getColumnIndex("Seqencing")) != -1)
                            modeList.setSeqencing(cursor.getInt(index));
                        if ((index = cursor.getColumnIndex("DeviceId")) != -1)
                            modeList.setDeviceId(cursor.getInt(index));
                        if ((index = cursor.getColumnIndex("ModeId")) != -1)
                            modeList.setModeId(cursor.getInt(index));
                        if ((index = cursor.getColumnIndex("BeginMonth")) != -1)
                            modeList.setBeginMonth(cursor.getString(index));
                        if ((index = cursor.getColumnIndex("EndMonth")) != -1)
                            modeList.setEndMonth(cursor.getString(index));
                        if ((index = cursor.getColumnIndex("ModeWeek")) != -1)
                            modeList.setModeWeek(cursor.getInt(index));
                        if ((index = cursor.getColumnIndex("ModeDay")) != -1)
                            modeList.setModeDay(cursor.getInt(index));
                        if ((index = cursor.getColumnIndex("LanguageId")) != -1)
                            modeList.setLanguageId(cursor.getInt(index));
                        if ((index = cursor.getColumnIndex("CompanyId")) != -1)
                            modeList.setCompanyId(cursor.getInt(index));
                        if ((index = cursor.getColumnIndex("Data2")) != -1)
                            modeList.setData2(cursor.getString(index));
                        if ((index = cursor.getColumnIndex("Data1")) != -1)
                            modeList.setData1(cursor.getString(index));
                        list.add(modeList);
                        cursor.moveToNext();
                    }
                    cursor.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public static List<GateWay> getGateWay(SQLiteDatabase db) {
        List<GateWay> list = new ArrayList<GateWay>();
        if (db != null) {
            try {
                Cursor cursor = db.query(TABLE_GATEWAY, null, null, null, null, null, null);
                int resultCount = cursor.getCount();
                cursor.moveToFirst();
                for (int i = 0; i < resultCount; i++) {
                    GateWay gateWay = new GateWay();
                    int index;
                    //if((index=cursor.getColumnIndex("Id"))!=-1)
                    gateWay.setId(cursor.getInt(0));
                    if ((index = cursor.getColumnIndex("GatewayName")) != -1)
                        gateWay.setGatewayName(cursor.getString(index));
                    if ((index = cursor.getColumnIndex("GatewayID")) != -1)
                        gateWay.setGatewayID(cursor.getString(index));
                    if ((index = cursor.getColumnIndex("UserName")) != -1)
                        gateWay.setUserName(cursor.getString(index));
                    if ((index = cursor.getColumnIndex("UserPassWord")) != -1)
                        gateWay.setUserPassWord(cursor.getString(index));
                    if ((index = cursor.getColumnIndex("TypeId")) != -1)
                        gateWay.setTypeId(cursor.getInt(index));
                    if ((index = cursor.getColumnIndex("State")) != -1)
                        gateWay.setState(cursor.getString(index));
                    if ((index = cursor.getColumnIndex("IsCurrent")) != -1)
                        gateWay.setIsCurrent(cursor.getInt(index));
                    if ((index = cursor.getColumnIndex("CreationTime")) != -1)
                        gateWay.setCreationTime(cursor.getString(index));
                    if ((index = cursor.getColumnIndex("Seqencing")) != -1)
                        gateWay.setSeqencing(cursor.getInt(index));
                    if ((index = cursor.getColumnIndex("CompanyId")) != -1)
                        gateWay.setCompanyId(cursor.getInt(index));
                    if ((index = cursor.getColumnIndex("LanguageId")) != -1)
                        gateWay.setLanguageId(cursor.getInt(index));
                    if ((index = cursor.getColumnIndex("Data1")) != -1)
                        gateWay.setData1(cursor.getString(index));
                    if ((index = cursor.getColumnIndex("Data2")) != -1)
                        gateWay.setData2(cursor.getString(index));
                    list.add(gateWay);
                    cursor.moveToNext();
                }
                cursor.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    public static List<User> getUser(SQLiteDatabase db) {
        List<User> list = new ArrayList<User>();
        if (db != null) {
            try {
                Cursor cursor = db.query(TABLE_USER, null, null, null, null, null, null);
                int resultCount = cursor.getCount();
                cursor.moveToFirst();
                for (int i = 0; i < resultCount; i++) {
                    User user = new User();
                    int index;
                    //if((index=cursor.getColumnIndex("id"))!=-1)
                    user.setId(cursor.getInt(0));
                    if ((index = cursor.getColumnIndex("Image")) != -1)
                        user.setUser_image(cursor.getString(index));
                    if ((index = cursor.getColumnIndex("Name")) != -1)
                        user.setUserName(cursor.getString(index));
                    if ((index = cursor.getColumnIndex("Pwd")) != -1)
                        user.setUserPassWord(cursor.getString(index));
                    if ((index = cursor.getColumnIndex("GradID")) != -1)
                        user.setGrad_ID(cursor.getInt(index));
                    if ((index = cursor.getColumnIndex("LoginType")) != -1)
                        user.setLoginType(cursor.getInt(index));
                    if ((index = cursor.getColumnIndex("CreatTime")) != -1)
                        user.setCreationTime(cursor.getString(index));
                    if ((index = cursor.getColumnIndex("Seqencing")) != -1)
                        user.setSeqencing(cursor.getInt(index));
                    if ((index = cursor.getColumnIndex("Nick")) != -1)
                        user.setUserNick(cursor.getString(index));
                    list.add(user);
                    cursor.moveToNext();
                }
                cursor.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    public static List<Floor> getFloor(SQLiteDatabase db) {
        List<Floor> list = new ArrayList<Floor>();
        if (db != null) {
            try {
                Cursor cursor = db.query(TABLE_FLOOR, null, null, null, null, null, null);
                int resultCount = cursor.getCount();
                cursor.moveToFirst();
                for (int i = 0; i < resultCount; i++) {
                    Floor floor = new Floor();
                    int index;
                    //if((index=cursor.getColumnIndex("id"))!=-1)
                    floor.setId(cursor.getInt(0));
                    if ((index = cursor.getColumnIndex("floorName")) != -1)
                        floor.setFloorName(cursor.getString(index));
                    if ((index = cursor.getColumnIndex("floorImage")) != -1)
                        floor.setFloorImage(cursor.getString(index));
                    if ((index = cursor.getColumnIndex("floorId")) != -1)
                        floor.setFloorId(cursor.getInt(index));
                    if ((index = cursor.getColumnIndex("CreationTime")) != -1)
                        floor.setCreationTime(cursor.getString(index));
                    if ((index = cursor.getColumnIndex("LanguageId")) != -1)
                        floor.setLanguageId(cursor.getString(index));
                    if ((index = cursor.getColumnIndex("Seqencing")) != -1)
                        floor.setSeqencing(cursor.getInt(index));
                    if ((index = cursor.getColumnIndex("IsShow")) != -1)
                        floor.setIsShow(cursor.getInt(index));
                    if ((index = cursor.getColumnIndex("avtar")) != -1)
                        floor.setAvtar(cursor.getString(index));
                    if ((index = cursor.getColumnIndex("graffitIndex")) != -1)
                        floor.setGraffitIndex(cursor.getString(index));
                    list.add(floor);
                    cursor.moveToNext();
                }
                cursor.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    public static int insertRoom(SQLiteDatabase db, Room room) {
        int num = 0;
        try {
            Cursor result = db.query(TABLE_ROOM, new String[]{"RoomName"},
                    "id=?", new String[]{room.getId() + ""}, null,
                    null, null, null);

            if (result.getCount() > 0) {
                num = 1;
            } else {
                ContentValues contentValues = new ContentValues();
                if (room.getId() != 0) {
                    contentValues.put("Id", room.getId());
                }
                contentValues.put("RoomName", room.getRoomName());
                contentValues.put("RoomImage", room.getRoomImage());
                contentValues.put("CreationTime", room.getCreationTime());
                contentValues.put("IsShow",
                        room.getIsShow());
                contentValues.put("Seqencing", room.getSeqencing());
                contentValues.put("Data1", room.getData1());
                contentValues.put("floorId", room.getFloorId());
                contentValues.put("floorName", room.getFloorName());
                num += db.insert(TABLE_ROOM, null, contentValues);
            }
            result.close();
        } catch (Exception e) {
            System.out.println(e.toString());
            num = -1;
        }
        return num;
    }

    public static int deleteAllRoom(SQLiteDatabase db) {
        int num;
        try {
            num = db.delete(TABLE_ROOM, null, null);
        } catch (Exception e) {
            System.out.println(e.toString());
            num = -1;
        }
        return num;
    }

    public static int insertDevice(SQLiteDatabase db, Device device) {
        int num = 0;
        try {
            Cursor cursor = db.query(TABLE_DEVICE, null,
                    "Id=?", new String[]{device.getId() + ""}, null, null, null);
            if (!cursor.moveToFirst()) {
                ContentValues contentValues = new ContentValues();
                if (device.getId() != 0) {
                    contentValues.put("Id", device.getId());
                }
                contentValues.put("DeviceName", device.getDeviceName());
                contentValues.put("DeviceImage", "");
                contentValues.put("DeviceID", device.getDeviceID());
                contentValues.put("deviceNickName", device.getDeviceNickName());
                contentValues.put("CreationTime", device.getCreationTime());
                contentValues.put("Seqencing", device.getSeqencing());
                contentValues.put("DeviceOnLine", device.getDeviceOnLine());
                contentValues.put("DeviceTimer", device.getDeviceTimer());
                contentValues.put("DeviceIO", device.getDeviceIO());
                contentValues.put("DeviceOrdered", device.getDeviceOrdered());
                contentValues.put("StartTime", device.getStartTime());
                contentValues.put("EndTime", device.getEndTime());
                contentValues.put("SceneId", device.getSceneId());
                contentValues.put("DeviceTypeKey", device.getDeviceTypeKey());
                contentValues.put("ProductsKey", device.getProductsKey());
                contentValues.put("GradingId", device.getGradingId());
                contentValues.put("RoomId", device.getRoomId());
                contentValues.put("CompanyId", device.getCompanyId());
                contentValues.put("LanguageId", device.getLanguageId());
                contentValues.put("RoleId", device.getRoleId());
                contentValues.put("Data1", device.getData1());
                contentValues.put("Data2", device.getData2());
                contentValues.put("floorID", device.getFloorId());
                contentValues.put("floorName", device.getFloorName());
                contentValues.put("allOpen", device.getAllOpen());
                contentValues.put("allClose", device.getAllClose());
                // YYX: 2018/11/22 修改后测试是否会不能添加
                num += db.insert(TABLE_DEVICE, null, contentValues);
            }
            cursor.close();
        } catch (Exception e) {
            // YYX: 2018/11/12 测试插入数据库失败？
            CrashReport.postCatchedException(e);
            num = -1;
        }
        return num;
    }


    public static int insertInfrad(SQLiteDatabase db, RedInfra infrared) {
        int num = 0;
        try {
            Cursor cursor = db.query(TABLE_INFRARED, null,
                    "Id=?", new String[]{infrared.getId() + ""}, null, null, null);
            if (!cursor.moveToFirst()) {
                ContentValues contentValues = new ContentValues();
                if (infrared.getId() != 0) {
                    contentValues.put("Id", infrared.getId());
                }
                contentValues.put("InfraredName", infrared.getInfraredName());
                contentValues.put("InfraredImage", infrared.getInfraredImage());
                contentValues.put("DeviceId", infrared.getDeviceId());
                contentValues.put("FatherId", infrared.getFatherId());
                contentValues.put("InfraredIndex", infrared.getInfraredIndex());
                contentValues.put("InfraredBrandId", infrared.getInfraredBrandId());
                contentValues.put("InfraredBrandName", infrared.getInfraredBrandName());
                contentValues.put("InfraredVersionId",
                        infrared.getInfraredVersionId());
                contentValues.put("InfraredVersionName",
                        infrared.getInfraredVersionName());
                contentValues.put("InfraredKey",
                        infrared.getInfraredKey());
                contentValues.put("InfraredButtonId", infrared.getInfraredButtonId());
                contentValues.put("InfraredCode", infrared.getInfraredCode());
                contentValues.put("InfraredStudyType", infrared.getInfraredStudyType());
                contentValues.put("InfraredX", infrared.getInfraredX());
                contentValues.put("InfraredY", infrared.getInfraredY());
                contentValues.put("CreationTime", infrared.getCreationTime());
                contentValues.put("Seqencing", infrared.getSeqencing());
                num += db.insert(TABLE_INFRARED, null, contentValues);
            }
            cursor.close();
        } catch (Exception e) {
            num = -1;
        }
        return num;
    }

    public static int insertMode(SQLiteDatabase db, Mode mode) {
        int num = 0;
        try {
            Cursor cursor = db.query(TABLE_MODE, null,
                    "Id=?", new String[]{mode.getId() + ""}, null, null, null);
            if (!cursor.moveToFirst()) {
                ContentValues contentValues = new ContentValues();
                if (mode.getId() != 0) {
                    contentValues.put("Id", mode.getId());
                }
                contentValues.put("Data1", mode.getData1());
                contentValues.put("Data2", mode.getData2());
                contentValues.put("LanguageId", mode.getLanguageId());
                contentValues.put("CompanyId", mode.getCompanyId());
                contentValues.put("ModeName", mode.getModeName());
                contentValues.put("ModeImage", mode.getModeImage());
                contentValues.put("ModeType", mode.getModeType());
                contentValues.put("ModeCode", mode.getModeCode());
                contentValues.put("Seqencing", mode.getSeqencing());
                contentValues.put("CreationTime", mode.getCreationTime());
                contentValues.put("StartTime", mode.getStartTime());
                contentValues.put("EndTime", mode.getEndTime());
                contentValues.put("ModeLoop", mode.getModeLoop());
                contentValues.put("ModeNickName", mode.getModeNickName());
                contentValues.put("GatewayID", mode.getGatewayID());
                contentValues.put("IsShow", mode.getIsShow());
                contentValues.put("roomId", mode.getRoomId());
                contentValues.put("roomName", mode.getRoomName());
                contentValues.put("floorName", mode.getFloorName());
                contentValues.put("floorId", mode.getFloorId());
                num += db.insert(TABLE_MODE, null, contentValues);
                // }
            }
            cursor.close();
        } catch (Exception e) {
            System.out.println(e.toString());
            num = -1;
        }
        return num;
    }

    public static int insertModeList(SQLiteDatabase db, ModeList modeList) {
        int num = 0;
        try {
            Cursor cursor = db.query(TABLE_MODE_LIST, null,
                    "Id=?", new String[]{modeList.getId() + ""}, null, null, null);
            if (!cursor.moveToFirst()) {
                ContentValues contentValues = new ContentValues();
                if (modeList.getId() != 0) {
                    contentValues.put("Id", modeList.getId());
                }

                contentValues.put("ModeAction", modeList.getModeAction());
                contentValues.put("ModeFunction", modeList.getModeFunction());
                contentValues.put("ModeTime", modeList.getModeTime());
                contentValues.put("ModeDelayed", modeList.getModeDelayed());
                contentValues.put("ModePeriod", modeList.getModePeriod());
                contentValues.put("CreationTime", modeList.getCreationTime());
                contentValues.put("DeviceId", modeList.getDeviceId());
                contentValues.put("Seqencing", modeList.getSeqencing());
                contentValues.put("ModeId", modeList.getModeId());
                contentValues.put("BeginMonth", modeList.getBeginMonth());
                contentValues.put("EndMonth", modeList.getEndMonth());
                contentValues.put("LanguageId", 1);
                contentValues.put("CompanyId", 1);
                contentValues.put("Data1", "0");
                contentValues.put("Data2", "0");

                num = (int) db.insert(TABLE_MODE_LIST, null, contentValues);
            }
            cursor.close();
        } catch (Exception e) {
            num = -1;
        }
        return num;
    }

    public static int insertGateWay(SQLiteDatabase db, GateWay gateWay) {
        int num = 0;
        if (db != null) {
            try {
                ContentValues contentValues = new ContentValues();
                if (gateWay.getId() != 0) {
                    contentValues.put("Id", gateWay.getId());
                }
                contentValues.put("GatewayName", gateWay.getGatewayName());
                contentValues.put("GatewayID", gateWay.getGatewayID());
                contentValues.put("UserName", gateWay.getUserName());
                contentValues.put("UserPassWord", gateWay.getUserPassWord());
                contentValues.put("TypeId", gateWay.getTypeId());
                contentValues.put("CreationTime", gateWay.getCreationTime());
                contentValues.put("IsCurrent", gateWay.getIsCurrent());
                contentValues.put("State", gateWay.getState());
                contentValues.put("Seqencing", gateWay.getSeqencing());
                num += db.insert(TABLE_GATEWAY, null, contentValues);
            } catch (Exception e) {
                System.out.println(e.toString());
            }
        }
        return num;
    }

    public static int insertUser(SQLiteDatabase db, User user) {
        int num = 0;
        if (db != null) {
            try {
                //Ling 2019.8.16 修复从权限转移下载的专案也没有权限的bug
                ContentValues contentValues = new ContentValues();
                contentValues.put("Image", user.getUser_image());
                contentValues.put("Name", user.getUserName());
                contentValues.put("Pwd", user.getUserPassWord());
                contentValues.put("GradID", user.getGrad_ID());
                contentValues.put("LoginType", user.getLoginType());
                contentValues.put("CreatTime", user.getCreationTime());
                contentValues.put("Seqencing", user.getSeqencing());
                contentValues.put("Nick", user.getUserNick());
                num += db.insert(TABLE_USER, null, contentValues);
            } catch (Exception e) {
                System.out.println(e.toString());
                num = 0;
            }
        }
        return num;
    }

    public static int insertFloor(SQLiteDatabase db, Floor floor) {
        int num = 0;
        if (db != null) {
            try {
                Cursor cursor = db.query(TABLE_FLOOR, null,
                        "Id=?", new String[]{floor.getId() + ""}, null, null, null);
                if (!cursor.moveToFirst()) {
                    ContentValues contentValues = new ContentValues();
                    if (floor.getId() != 0) {
                        contentValues.put("Id", floor.getId());
                    }
                    contentValues.put("floorName", floor.getFloorName());
                    contentValues.put("floorImage", floor.getFloorImage());
                    contentValues.put("floorId", floor.getFloorId());
                    contentValues.put("CreationTime", floor.getCreationTime());
                    contentValues.put("LanguageId", floor.getLanguageId());
                    contentValues.put("Seqencing", floor.getSeqencing());
                    contentValues.put("IsShow", floor.getIsShow());
                    contentValues.put("avtar", floor.getAvtar());
                    contentValues.put("graffitIndex", floor.getGraffitIndex());
                    contentValues.put("nikeName", floor.getNikeName());
                    num += db.insert(TABLE_FLOOR, null, contentValues);
                }
                cursor.close();
            } catch (Exception e) {
                System.out.println(e.toString());
            }
        }
        return num;
    }


}
